package dialogs;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import controllers.AbstractController;

@SuppressWarnings("serial")
public class ProcessTransactionDialog extends AbstractGeneralPurposeDialog {

	public ProcessTransactionDialog(JFrame parent, String title, String table,
			String[] labels, AbstractController c) {
		super(parent, title, table, labels, c);
	}

	protected void insertButtons(JPanel buttonPane)
	{
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.X_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(10, 0, 0, 2));

		JButton OKButton = new JButton("OK");
		JButton cancelButton = new JButton("Cancel");
		JButton addItemButton = new JButton("Add Another Item");
		
		OKButton.addActionListener(this);
		OKButton.setActionCommand("OK");
		
		addItemButton.setActionCommand("AddItem");
		addItemButton.addActionListener(this);
		
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				close_dialog();
			}
		});

		// add the buttons to buttonPane
		buttonPane.add(Box.createHorizontalGlue());
		buttonPane.add(OKButton);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(cancelButton);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		buttonPane.add(addItemButton);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String actionCommand = e.getActionCommand();
		Vector<String> s = new Vector<String>();
		for (JTextField j : text_fields)
			s.add(j.getText());

		if (actionCommand.equals("OK")) {
			int retCode = controller.validateInsert(s);
			if (retCode == OPERATIONSUCCESS) {
				dispose();
				controller.show();
			} else if (retCode == VALIDATIONERROR) {
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(this, "Invalid Input", "Error",
						JOptionPane.ERROR_MESSAGE);
			} else {
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(this,
						"UPC Doesn't exist, or quantity is too high", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		} else {
			int retCode = controller.validateEntries(s);
			if (retCode == OPERATIONSUCCESS) {
				for (JTextField j : text_fields)
					j.setText("");
			} else if (retCode == VALIDATIONERROR) {
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(this, "Invalid Input", "Error",
						JOptionPane.ERROR_MESSAGE);
			} else {
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(this,
						"UPC Doesn't exist, or quantity is too high", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
